import { AppUtils } from '../api/AppUtils';
import { Component, ViewChild , OnInit, AfterViewChecked } from '@angular/core';
import { ApiService } from '../api/api.service';
import { Router } from '@angular/router';
import { ApiUrl } from '../api/ApiUrl';
import { ArticleVO } from '../vo/ArticleVO';
import { Slides } from '@ionic/angular';
import { AdvertiseVO } from '../vo/AdvertiseVO';
import { ToastController, Events } from '@ionic/angular';
import { UserVO } from '../vo/UserVO';
@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit, AfterViewChecked{
  @ViewChild(Slides) slides: Slides;
  queryText = '';
  dtxxItems: ArticleVO[] = [];
  userItem: UserVO;
  slideItems: AdvertiseVO[] = [
    { 'iid': 1, 'ccontent': 'title1', 'cpicpath': 'assets/lb/lb02.png', 'cskiplinks': '' },
    { 'iid': 2, 'ccontent': 'title2', 'cpicpath': 'assets/lb/lb01.png', 'cskiplinks': '' },
    { 'iid': 3, 'ccontent': 'title3', 'cpicpath': 'assets/lb/lb03.png', 'cskiplinks': '' }
  ];

  constructor(private toastController: ToastController, private api: ApiService, private router: Router, private appUtils: AppUtils) {
    // 加载动态信息
    this.api.getItems<ArticleVO>(ApiUrl.dtxx, {}).subscribe(response => {
      if (response === null || response.length === 0) {
        console.log('没有数据返回');
        return;
      }
      this.dtxxItems = response;
      console.log(this.dtxxItems);
    });
    // 加载轮播图信息
    this.api.getItems<AdvertiseVO>(ApiUrl.lbt, { 'ilocation': 1 }).subscribe(response => {
      if (response != null || response.length > 0) {
        this.slideItems = response;
        // 遍历拼接url
        this.slideItems.forEach(element => {
          element.cpicpath = ApiUrl.baseUrl + element.cpicpath;
        });
      }
    });
  }
  ngOnInit() {
    if (ApiUrl.user != null) {
      this.userItem = ApiUrl.user;
    } else {
      this.userItem = null;
    }
  }
  ngAfterViewChecked() {
    if (ApiUrl.user != null) {
      this.userItem = ApiUrl.user;
    } else {
      this.userItem = null;
    }
  }
  ionViewDidEnter() {
    // 修复轮播手动滑动后不能自动轮播问题
    this.slides.startAutoplay();
  }

  pushSearchPage(): void {
    this.router.navigate(['/search', { 'queryText': this.queryText }]);
  }

  goTocompanydynamic(itype): void {
    this.router.navigate(['/companydynamic', { 'itype': itype }]);
  }
  goToPolicy(): void {
    this.router.navigate(['/companydynamic', { 'itype': 2 }]);
  }
  goToDetails(itype) {
    this.router.navigate(['/companymore', { 'itype': itype }]);
  }
  // 打开浏览器页面
  toWebView(curl, ctitle) {
    this.appUtils.toWebView(curl, ctitle);
  }
  // 金融产品跳转页面
  toFinancialProduct() {
    this.toWebView('http://xz.jsjrfw.com/', '金融产品');
  }

  visit() {
    if ( this.userItem != null) {
      console.log(this.userItem.iusertype);
      if (this.userItem.iusertype + '' === '1' || this.userItem.iusertype + '' === '2') {
        this.router.navigate(['/visit']);
      } else {
        this.presentToast('只有金融顾问有权限访问！');
      }
    } else {
      this.presentToast('只有金融顾问有权限访问！');
    }
  }
  connect() {
    if ( this.userItem == null ) {
      this.router.navigate(['/connectcount']);
    } else {
      if (this.userItem.iusertype + '' === '1' || this.userItem.iusertype + '' === '2') {
        this.router.navigate(['/connect']);
      } else {
        this.router.navigate(['/connectcount']);
      }
    }
  }
  async presentToast(msg) {
    const toast = await this.toastController.create({
      message: msg,
      duration: 2000
    });
    toast.present();
  }
  goToDetails1 (iid) {
    this.router.navigate(['/article', { 'iid': iid }]);
  }
}
